﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Diagnostics;
using MixScript.DBService;

namespace MixScript.Appl.Message
{
    public class ErrMessage : cDBService
    {
        private static bool isFromDB = false;
        private const string XmlErrPath = "D:\\yanusapu\\Development\\2010\\MixScriptWeb\\MixScriptWeb\\App_Data\\config\\ErrXml.xml";


        public ErrMessage()
        {
        }

        public string getError(Int32 errorCode)
        {
            
            return getError(errorCode.ToString().PadLeft(6,'0'),"");
        }
        public string getError(string errorCode, string langid)
        {
            try
            {
                if (langid.ToString().Trim() == string.Empty)
                {
                    langid = "EN";
                }

                if (isFromDB)
                {
                }
                else
                {
                    //message errcode="999999" 
                    XElement root = XElement.Load(XmlErrPath);
                    IEnumerable<XElement> language =
                        from el in root.Elements("language")
                        where (string)el.Attribute("langid") == langid
                        select el;

                    IEnumerable<XElement> msg =
                                        from ell in language.Elements("message")
                                        where (string)ell.Attribute("errcode") == errorCode
                                        select ell;
                    if (msg.Count() > 0)
                    {
                        foreach (XElement el in msg)
                        {
                            return el.Attribute("errmsg").Value;
                            //Debug.WriteLine(el);
                        }


                    }
                    else
                    {
                        IEnumerable<XElement> msg1 =
                                        from ell in language.Elements("message")
                                        where (string)ell.Attribute("errcode") == "999999"
                                        select ell;
                        foreach (XElement el in msg1)
                        {
                            return el.Attribute("errmsg").Value + " - Error Code: "+errorCode;
                            //Debug.WriteLine(el);
                        }

                        return "Error 999999 " + langid;
                        
                    }



                }
                return "";

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

    }
}